<template>
  <div>
<!--    <div class="toolbar">-->
<!--      <el-form :inline="true" :model="q" size="mini">-->
<!--        <el-form-item label="订单ID:">-->
<!--          <el-input v-model="q.id" onkeyup="value=value.replace(/[^\d]/g,'')" placeholder="订单ID"-->
<!--                    prefix-icon="el-icon-edit"></el-input>-->
<!--        </el-form-item>-->

<!--        <el-form-item>-->
<!--          <el-button type="primary" @click="query">查询</el-button>-->
<!--          <el-button type="danger" @click="reload">重置</el-button>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--    </div>-->
    <el-row type="flex" class="row-bg btn-row" justify="end">
      <el-button size="mini" @click="add" type="primary" icon="el-icon-plus">添加快捷回复</el-button>
    </el-row>
    <el-table :data="listData.items" v-loading.body="listLoading" @sort-change="sortlist" size="mini" stripe border fit
              highlight-current-row>
      <el-table-column prop="id" label="ID" width="80" fixed="left"></el-table-column>
      <el-table-column prop="title" label='标题' width="150"></el-table-column>
      <el-table-column prop="content" label='内容' width="370"></el-table-column>
      <el-table-column prop="created_at" label='创建时间' width="180"></el-table-column>
      <el-table-column prop="updated_at" label='修改时间' width="180"></el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" size="mini" @click="update(scope.row)">编辑</el-button>
            <el-button type="warning" size="mini" @click="deleteData(scope.row)">删除</el-button>

          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="q.page_no" :page-sizes="$store.state.app.pageSizeArr"
                     :page-size="q.page_size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="listData.total">
      </el-pagination>
    </div>
    <save-update v-if="showUpdate" :showUpdate.sync="showUpdate" :id="show_id" :DetailInfo="DetailInfo"
    ></save-update>
  </div>
</template>

<script>
  import saveUpdate from './saveUpdate'
  import {getQuickReplyList, deleteQuickReplyById} from '@/api'
  import {queryFilter} from '@/utils/patch'

  export default {
    components: {saveUpdate},
    data() {
      return {
        listData: {},
        q: {page_size: 30, page_no: 1},
        listLoading: false,
        Timetype: true,
        showUpdate: false,
        ReChargeStatus: this.$store.state.app.ReChargeStatus,
        show_id: null,
        DetailInfo: {}
      }
    },
    activated() {
      this.getList()
    },
    deactivated() {
      this.listLoading = false
    },
    methods: {
      formatAgency() {
        return this.$store.state.user.userinfo.user_name || this.$store.state.user.userinfo.nickname;
      },
      add() {
        this.show_id = null
        this.showUpdate = true
      },
      deleteData(row) {
        this.$confirm(`确定删除ID：${row.id} 的快捷回复吗？`, '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteQuickReplyById(row.id).then(res => {
            if (res.status === 0) {
              this.$message({message: '删除成功', type: 'success'})
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      update(row) {
        this.show_id = row.id
        this.showUpdate = true
        this.DetailInfo = {title: row.title, content: row.content}
      },
      query() {
        this.q = Object.assign(this.q, {page_no: 1, page_size: 30})
        this.getList()
      },
      reload() {
        this.q = {page_size: 30, page_no: 1}
        this.getList()
      },
      toggleTimetype() {
        if (this.Timetype) {
          delete this.q.created_at_begin
          delete this.q.created_at_end
          this.Timetype = !this.Timetype
        } else {
          delete this.q.updated_at_begin
          delete this.q.updated_at_end
          this.Timetype = !this.Timetype
        }
      },
      sortlist(sort) {
        this.q.$orderby = sort.prop
        this.getList()
      },
      handleSizeChange(val) {
        this.q.page_size = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.q.page_no = val
        this.getList()
      },
      getList() {
        this.q = queryFilter(this.q)
        getQuickReplyList(this.q).then(res => {
          if (res.status === 0) {
            this.listLoading = false
            this.listData = res.data
          }
        })
      }
    },
    watch: {
      showUpdate() {
        if (!this.showUpdate) {
          this.getList()
        }
      }
    }
  }
</script>
